//
//  ViewController.m
//  TelBook
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import"SecondViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,SecondViewControllerDelegate>
@property (nonatomic,retain)NSMutableDictionary *dictionary;
@property (nonatomic,retain)NSArray *keysArray;
@property(nonatomic ,retain)UITableViewCell  *cell;
@property(nonatomic,retain)UITableView *tableView;
@end

@implementation ViewController
-(void)dealloc{
    [_dictionary release];
    [_keysArray release];
    [super dealloc];
}

- (void)viewDidLoad {

    self.dictionary = [[NSMutableDictionary alloc]init];

    self.keysArray  = [[NSArray alloc]init];
    
    
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationItem.title = @"联系人";
    self.automaticallyAdjustsScrollViewInsets =NO;
    [self easeatbbq];
    [self handeData];
}
-(void)handeData{

    NSString *path = [[NSBundle mainBundle]pathForResource:@"Pro" ofType:@".plist"];

    self.dictionary  = [NSMutableDictionary dictionaryWithContentsOfFile:path];
   self.keysArray = [self.dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
}









-(void)easeatbbq{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64,CGRectGetWidth(self.view.bounds) ,CGRectGetHeight(self.view.bounds) -64) style:UITableViewStylePlain];
   self.tableView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_tableView];
    [_tableView release];
   self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 100;
    
    [self.tableView  registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dictionary.count;
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.keysArray objectAtIndex:section];
    NSArray *array = [self.dictionary objectForKey:key];
    
    return array.count;
    
    
   
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    self.cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    self.cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    
    

    NSArray *array  =[ self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    

    
    self.cell.textLabel.text = [dic objectForKey:@"name"];
    
    self.cell.detailTextLabel.text = [dic objectForKey:@"number"];
    
    
    
    self.cell.imageView.image = [self reSizeImage:[UIImage imageNamed:[dic objectForKey:@"poto"]] andSize:CGSizeMake(tableView.rowHeight, tableView.rowHeight)];
    
    
    
    
    
    
    
//    self.cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"poto"]];
  
    self.cell.imageView.layer.cornerRadius = tableView.rowHeight/2;
    self.cell.imageView.clipsToBounds =YES;

    
    self.cell.accessoryType =UITableViewCellAccessoryDetailButton;
    
    return _cell;
    
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *headerString = [self.keysArray objectAtIndex:section];
    
    
    
    return headerString;
}
-(NSArray<NSString*>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keysArray;
    
}
    
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    SecondViewController *sec = [[SecondViewController alloc]init];
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    NSDictionary *dic  = [ array objectAtIndex:indexPath.row];
    sec.textfidKey = [dic objectForKey:@"name"];
    sec.QQKey = [dic objectForKey:@"QQ"];
    sec.telKey  = [dic objectForKey:@"number"];
    sec.imagekey = [dic objectForKey:@"poto"];
    sec.indexPath = indexPath;
    sec.delegate = self;
    
    [self.navigationController pushViewController:sec animated:YES];
    
    
}

- (UIImage *) reSizeImage:(UIImage *)image andSize:(CGSize)reSize {
    
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));
    
    [image drawInRect:CGRectMake(0, 0, reSize.width,reSize.height)];
    
    UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return reSizeImage;
}

-(void)withName:(NSString *)name withQq:(NSString *)qq withTel:(NSString *)tel withImage:(UIImageView *)image withIndexPath:(NSIndexPath *)indexPath{
//    SecondViewController *sec  = [[SecondViewController alloc]init];
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    [dic setValue:name forKey:@"name"];
    [dic setValue:qq forKey:@"QQ"];
    [dic setValue:tel forKey:@"number"];
    [dic setValue:image forKey:@"image"];
    [self.tableView reloadData];
    
    
    
}

//-(void)withName:(NSString *)name withQq:(NSString *)qq withTel:(NSString *)tel withImage:(UIImageView *)image withIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%@",name);
//}




@end
